package freejim.icu.highschoolhealthstatuspost.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.util.IOUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import freejim.icu.highschoolhealthstatuspost.dao.DailyPost;
import freejim.icu.highschoolhealthstatuspost.dao.DetailInfo;
import freejim.icu.highschoolhealthstatuspost.dao.Info;
import freejim.icu.highschoolhealthstatuspost.dao.User;
import freejim.icu.highschoolhealthstatuspost.service.AnalyseService;
import freejim.icu.highschoolhealthstatuspost.service.StatusService;
import freejim.icu.highschoolhealthstatuspost.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.supercsv.io.CsvBeanWriter;
import org.supercsv.io.ICsvBeanWriter;
import org.supercsv.prefs.CsvPreference;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class InfoController {

    @Autowired
    StatusService statusService;

    @Autowired
    UserService userService;

    @Autowired
    AnalyseService analyseService;

    @RequestMapping("/user/health-post")
    @ResponseBody
    public String healthPost(HttpServletRequest request){
        if (!statusService.recordPost(request)){
            return "repeat";
        }
        return "success";
    }

    @RequestMapping("/admin/getOverview")
    @ResponseBody
    public Object getOverview(){
        long postNum = statusService.getTodayPostNum();
        long unPostNum = statusService.getUserNum() - postNum;
        List<Info> unPostList = statusService.getUnPostList();
        List<Info> postList = statusService.getPostList();
        Map<String,Object> result = new HashMap<>();
        result.put("postnum",postNum);
        result.put("unpostnum",unPostNum);
        result.put("unpostList",unPostList);
        result.put("postList",postList);
        return result;
    }

    static Map<Integer,String> licenceType = null;

    @RequestMapping("/admin/getDetailInfoById")
    @ResponseBody
    public Object getDetailInfoById(@RequestParam("id") long id){
        if (licenceType == null){
            licenceType = new HashMap<>();
            licenceType.put(1,"身份证");
            licenceType.put(2,"居住证");
            licenceType.put(3,"护照");
        }
        DetailInfo detailInfo = statusService.getDetailInfoById(id);
        List<Map<String,Object>> results = new ArrayList<>();
        Map<String,Object> tempResultData = new HashMap<>();
        tempResultData.put("detailInfo",detailInfo);
        results.add(tempResultData);
        tempResultData = new HashMap<>();
        tempResultData.put("licenceType",licenceType.get(detailInfo.getLicence_type()));
        results.add(tempResultData);
        return results;
    }

    @RequestMapping("/admin/getUserList")
    @ResponseBody
    public Object getUserList(){
        return userService.getUserList();
    }

    @RequestMapping("/admin/data.csv")
    @ResponseBody
    public void getExcel(@RequestBody JSONArray array, HttpServletResponse response) throws IOException {
        System.out.println(array);
        String csvContent = analyseService.buildCSV(array);
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=data.csv");
        OutputStream out = response.getOutputStream();
        out.write(csvContent.getBytes());
    }
}
